﻿//前端通过获取后端公钥进行RSA加密  (前端公钥加密，后端私钥解密)
function RSAEncrypt(word, publicKey) {
	var pub = KEYUTIL.getKey(publicKey);
	var data = KJUR.crypto.Cipher.encrypt(word,pub);
	console.log(data);
    console.log(hextob64(data));
	return data;
}

//前端通过获取后端私钥进行RSA解密 (前端私钥解密，后端公钥加密)
function RSADecrypt(word, privateKey) {
	var prv = KEYUTIL.getKey(privateKey);
	var data = KJUR.crypto.Cipher.decrypt(word,prv);
	console.log("jsrsasign decrypt: "+data);
	return data;
}

//RSA私钥签名
function RSAEncryptByPre(word, privateKey) {
	// 创建 Signature 对象
	let signature=new KJUR.crypto.Signature({alg:"SHA1withRSA",prvkeypem:privateKey});    //!这里指定 私钥 pem!
	signature.updateString(word);
	let a = signature.sign();
	let sign = hextob64(a);
	console.log(sign);
	return sign;
}

//RSA公钥验签
function RSADecryptByPub(word,sign,publicKey) {
	// 验签
	// !要重新new 一个Signature, 否则, 取摘要和签名时取得摘要不一样, 导致验签误报失败(原因不明)!
	let signatureVf = new KJUR.crypto.Signature({alg:"SHA1withRSA",prvkeypem:publicKey});
	console.log("jsrsasign verify: 1");
	signatureVf.updateString(word);
	console.log("jsrsasign verify: 2");
	// !接受的参数是16进制字符串!
	let data = signatureVf.verify(b64tohex(sign));
	console.log("jsrsasign verify: "+data);
	return data;
}